// 局部混入mixin_order.js文件

export default {
	data() {
		return {
			
		}
	},
	
	methods: {
		// 微信小程序支付预下单并付款
		payWxapplet(orderSn, page) {
			console.log('微信小程序支付预下单收到商家订单编号：', orderSn)
			this.$api.PayWxapplet({
				orderSn: orderSn,  // 商家订单编号
				openid: this.openid
			}).then(res => {
				console.log('payWxapplet', res)
				if (res.data.code == 200) {
					let obj = res.data.data.items
					uni.requestPayment({
						provider: "wxpay",
						timeStamp: obj.timestamp,
						nonceStr: obj.noncestr,
						package: 'prepay_id='+obj.prepayid,
						signType: 'MD5',
						paySign: obj.sign,
						success: (res) => {
							console.log('uni.requestPayment-success', res)
							uni.showToast({
								icon:"none",
								title:"支付成功..."
							})
						},
						fail: (err) => {
							console.log('uni.requestPayment-fail', err)
							uni.showToast({
								icon:"none",
								title:"支付已取消"
							})
						},
						complete: () => {
							this.afterPay(page)
						}
					})
				}else {
					return uni.showToast({
						icon:"none",
						title:res.data.msg,
						success: () => {
							this.afterPay(page)
						}
					})
				}
			}).catch(err => {
				console.log('payWxapplet', err)
				return uni.showToast({
					icon:"none",
					title:err.data.msg
				})
			})
		},
		
		// 支付后
		afterPay(page) {
			switch (page){
				case 'submit':
					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
							success: () => {
								uni.navigateTo({
									url: `/pages/subPackage/user/my_order/my_order?type=${0}`
								})
							},
						})
					}, 300)
					break;
				case 'list':
					uni.startPullDownRefresh()
					break;
				case 'detail':
					uni.startPullDownRefresh()
					break;
				default:
					uni.startPullDownRefresh()
					break;
			}
		},
		
		// 立即退款（未发货订单全额退款
		refundAll(id) {
			uni.showModal({
				content: "确定立即退款？",
				confirmColor: this.primaryColor,
				success: (res) => {
					// 用户点击确定
					if (res.confirm) {
						this.$api.RefundAll({
							merOrderNo: id
						}).then(res => {
							console.log('RefundAll-success', res)
							if (res.data.code === 200) {
								uni.startPullDownRefresh({
									success: () => {
										uni.showToast({
											icon: 'none',
											title: '退款成功！'
										})
									}
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								})
							}
						}).catch(err => {
							console.log('RefundAll-fail', err)
						})
					}
				}
			})
		},
		
		// 处理订单
		manageOrder(id, status, page) { // status	状态id(1=取消订单,2=确认收货,3=删除订单)
			let tips = ['取消', '收货', '删除']
			uni.showModal({
				content: `是否确定${tips[status-1]}？`,
				confirmColor: this.primaryColor,
				success: (res) => {
					if (res.confirm) {
						this.$api.ManageOrder({
							orderNo: id,
							status: status
						}).then(res => {
							console.log('ManageOrder-success', res)
							if (res.data.code == 200) {
								switch (page){
									case 'list':
										uni.startPullDownRefresh({
											success: () => {
												if (status==3) {
													uni.showToast({
														icon: 'none',
														title: '删除成功！'
													})
												}
											}
										})
										break;
									case 'detail':
										uni.navigateBack()
										break;
									default:
										break;
								}
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data.msg
								})
							}
						}).catch(err => {
							console.log('ManageOrder-fail', err)
							
						})
					}
				}
			})
		},
		
		// 跳转评价页面
		goReview(id, pet, merchantId) {
			uni.navigateTo({
				url: `/pages/subPackage/user/my_order_review/my_order_review?id=${id}`+`&merchantId=${merchantId}`+
				`&pet=${encodeURIComponent(JSON.stringify(pet))}`
			})
		},
		
		// 跳转申请换货页面
		goAftersaleApply(id, pet) {
			uni.navigateTo({
				url: `/pages/subPackage/user/my_order_aftersale_apply/my_order_aftersale_apply?id=${id}`+
				`&pet=${encodeURIComponent(JSON.stringify(pet))}`
			})
		},
		
		// 跳转上传物流信息页面
		goAftersaleExpress(id, pet, id2) {
			uni.navigateTo({
				url: `/pages/subPackage/user/my_order_aftersale_express/my_order_aftersale_express?id=${id}`+
				`&pet=${encodeURIComponent(JSON.stringify(pet))}`+`&id2=${id2}`
			})
		},
		
		// 不够10补0
		repair(i){
			if (i >= 0 && i <= 9) {
				return "0" + i
			} else {
				return i
			}
		},
		// 获取n天后日期
		getSpecialTime(date, n) {
			let arr = date.split('-')
			let year = Number(arr[0])
			let month = Number(arr[1])
			let day = Number(arr[2])
			switch (month){
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
					if (day+n <= 31) { // 未过月
						return year + '-' + this.repair(month) + '-' + this.repair(day+n)
					} else { // 过月
						return year + '-' + this.repair(month+1) + '-' + this.repair(day+n-31)
					}
					break;
				case 4:
				case 6:
				case 9:
				case 11:
					if (day+n <= 30) { // 未过月
						return year + '-' + this.repair(month) + '-' + this.repair(day+n)
					} else { // 过月
						return year + '-' + this.repair(month+1) + '-' + this.repair(day+n-30)
					}
					break;
				case 2:
					let num = year/4==0 ? 29 : 28
					if (day+n <= num) { // 未过月
						return year + '-' + this.repair(month) + '-' + this.repair(day+n)
					} else { // 过月
						return year + '-' + this.repair(month+1) + '-' + this.repair(day+n-num)
					}
					break;
				case 12:
					break;
					if (day+n <= 30) { // 未过月
						return year + '-' + this.repair(month) + '-' + this.repair(day+n)
					} else { // 过月
						return (year+1) + '-' + '01' + '-' + this.repair(day+n-30)
					}
				default:
					break;
			}
		},
	}
}